package research.sbookmarking.input;

import java.util.*;
import org.connotea.*;
import org.afox.util.*;
import research.sbookmarking.*;
import research.sbookmarking.process.*;

//import java.util.Random;


public class RandomUserCommand extends BookmarkCommand {

	//default number of users to query
	private int numUsers = 223;
	
	public RandomUserCommand(String queryName, List command) {
		
		super(queryName,command);
		
	}

	public RandomUserCommand(String queryName, List command, Integer limit, 
							 Integer start, Integer numUsers) {
		
		super(queryName,command, limit, start);
		this.numUsers = numUsers.intValue();
	}

/*
	private void add (ArrayList users, List list) {
		Iterator iterate = list.iterator();
		while (iterate.hasNext()){
			Object item = iterate.next();
			if (users.contains(item)!=true){
				users.add(users);
			}
		}
	}
*/	

	/* (non-Javadoc)
	 * @see research.sbookmarking.input.BookmarkCommand#execute()
	 */
	public void execute() {
		System.out.println ("First getting list then get users");
		super.execute();
		System.out.println ("Finished getting list now getting users");
		setQueryName("Random");
		DataContainer temp = getResult();
		
		ProcessController.getInstance().getUsers(temp);
		
		Connotea database = new Connotea ("alby1976","a1014leung");
		Random random = new Random();
		List users = (List)RMethod.invoke(temp, "getUniqueUsers", new ArrayList());

		if (this.numUsers > users.size()){
			this.numUsers = users.size();
		}
			
		for (int count=0; count < this.numUsers;count++){
			ArrayList results = new ArrayList();
			getAll (results, database, "getPosts",(String)users.get(random.nextInt(users.size())), getLimit(),getStart());
			
			GeneralContainer data = new GeneralContainer(getQueryName(),results);
			data.setUniqueUsers(users);
			setResult(data);
		}
	}
		
		
/*
		// get list of bookmarks
		super.execute();
		// get list of users
		ArrayList users = new ArrayList();
		Iterator lists = super.getResult().getQueryResults().iterator();
		while (lists.hasNext()) {
			this.add(users,((Post)lists.next()).getBookmark().getPostedBy());
		}
		
		ArrayList data = new ArrayList();
		Random random = new Random();
		
		try {
			Connotea database = new Connotea ("alby1976","a1014leung");
			ArrayList results = new ArrayList();
			for (int count=0; count <= this.numUsers;count++){
				//String user =(String)((List)getResult()).get(random.nextInt(users.size()));
				data.add(database.getBookmarks((String)users.get(random.nextInt(users.size()))));
			}
			setResult(new GeneralContainer(data));
		}
		catch (ConnoteaException e){
			System.err.println(e.getMessage());
			System.exit(-1);
		}
	}
*/
		
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
